//
//  UIBarButtonItem+Extension.swift
//  SinaBlog
//
//  Created by ChenWei on 15/10/4.
//  Copyright © 2015年 cw. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    /// 对象方法:创建并返回一个item
    func createBarButtonItem(imageName: String, target : AnyObject , action: Selector)-> UIBarButtonItem {
        let btn : UIButton = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
       return UIBarButtonItem(customView: btn)
    }
    
    
    /// 关方法:创建并返回一个item
    class func barButtonItem(imageName: String, target : AnyObject , action: Selector)-> UIBarButtonItem {
        let btn : UIButton = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
    
        return UIBarButtonItem().createBarButtonItem(imageName, target: self, action: action)
    }
    
    /// 便利构造方法:创建并返回一个item
    convenience init(imageName: String, target : AnyObject , action: Selector) {
        let btn : UIButton = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        self.init(customView:btn)
    }
}
